prepare($students_query);
$students_stmt->execute([$acyear, $term, $klass]);
$students = $students_stmt->fetchAll(PDO::FETCH_ASSOC);
// Get subjects
$subjects_query = "SELECT DISTINCT subject FROM marks
WHERE acyear = ?
AND term = ?
AND klass = ?
ORDER BY subject";
$subjects_stmt = $DBcon->prepare($subjects_query);
$subjects_stmt->execute([$acyear, $term, $klass]);
$subjects = $subjects_stmt->fetchAll(PDO::FETCH_ASSOC);
if(count($students) > 0 && count($subjects) > 0) {
// Calculate totals and averages for all students first
$student_data = [];
foreach($students as $student) {
$regno = $student['regno'];
$total_score = 0;
$subject_count = 0;
// Calculate total score for this student
foreach($subjects as $subject) {
$mark_query = $DBcon->prepare("SELECT test, exam FROM marks WHERE regno = ? AND subject = ? AND acyear = ? AND term = ? AND klass = ?");
$mark_query->execute([$regno, $subject['subject'], $acyear, $term, $klass]);
$mark = $mark_query->fetch(PDO::FETCH_ASSOC);
if($mark) {
$total_score += $mark['test'] + $mark['exam'];
$subject_count++;
}
}
$average = $subject_count > 0 ? $total_score / $subject_count : 0;
$student_data[] = [
'regno' => $regno,
'fullname' => $student['fullname'],
'total_score' => $total_score,
'average' => $average
];
}
// Sort students by average (descending) for position
usort($student_data, function($a, $b) {
return $b['average'] <=> $a['average'];
});
// Assign positions (handle ties)
$student_positions = [];
$current_position = 1;
$previous_average = null;
foreach($student_data as $index => $student) {
if($previous_average !== null && $student['average'] < $previous_average) {
$current_position = $index + 1;
}
$student_positions[$student['regno']] = $current_position;
$previous_average = $student['average'];
}
?>
Class Result Sheet
Print Result
Total Students
Total Subjects
TOTAL SCORE
AVERAGE
POSITION
Test
Exam
Total
prepare($students_query);
$students_display->execute([$acyear, $term, $klass]);
$students_for_display = $students_display->fetchAll(PDO::FETCH_ASSOC);
foreach($students_for_display as $student):
$regno = $student['regno'];
$position = $student_positions[$regno] ?? '-';
$position_class = '';
if($position == 1) $position_class = 'position-1';
elseif($position == 2) $position_class = 'position-2';
elseif($position == 3) $position_class = 'position-3';
?>
prepare("SELECT test, exam FROM marks WHERE regno = ? AND subject = ? AND acyear = ? AND term = ? AND klass = ?");
$mark_query->execute([$regno, $subject['subject'], $acyear, $term, $klass]);
$mark = $mark_query->fetch(PDO::FETCH_ASSOC);
if($mark):
$test = $mark['test'];
$exam = $mark['exam'];
$subject_total = $test + $exam;
$student_total += $subject_total;
$student_subject_count++;
?>
-
-
-
0 ? number_format($student_total / $student_subject_count, 2) : '0.00'; ?>
Class Size:
Students
Subjects Offered:
Subjects
Academic Year:
Term:
_________________________
Class Teacher
_________________________
Principal
_________________________
Date:
No results found for the selected criteria.
';
}
} catch(PDOException $e) {
echo '
Database error: ' . htmlspecialchars($e->getMessage()) . '